package com.zmj.sy.mom.srv.aps.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ApsWorkDetail;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ApsWorkOrder;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ReverseOrder;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.order.OrderSubOrderResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.weekwork.*;
import com.zmj.sy.mom.srv.aps.mapper.ApsWorkDetailMapper;
import com.zmj.sy.mom.srv.aps.mapper.ApsWorkOrderMapper;
import com.zmj.sy.mom.srv.aps.mapper.ReverseOrderMapper;
import com.zmj.sy.mom.srv.aps.utils.PkgUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class WeekWorkService {

    private final ApsWorkDetailMapper apsWorkDetailMapper;
    private final ApsWorkOrderMapper apsWorkOrderMapper;
    private final ReverseOrderMapper reverseOrderMapper;

    // 时间：默认当天所在周的一周计划，7天。如果有输入时间，则按照输入时间进行查询和展示，及每天计划
    // 工作区计划：
    //      整加区：P5各工序，P4的ZT、ZZ、PT，P3的ZT、ZZ，侧护板、挡板组件的PT工序
    //      焊接区：P1、P2、P3、P4、侧护板、挡板组件的PZ、WH
    //      配套区：P1、P2、P3、侧护板、挡板组件的PZ工序提前8小时
    //      下料区：P1、侧护板、挡板组件的PZ工序提前24小时

    // 查询逻辑修改：将侧护板等非三大件独立出来，单独查询，构建展示信息
    // 二级页面传参增加订单ID，判断是否为侧护板订单，以此判断二级页面信息展示的查询逻辑

    public WeekWorkResVo getlist(WeekWorkReqVo reqVo) {
        WeekWorkResVo resVo = new WeekWorkResVo();
        LocalDate startTime = reqVo.getStartTime();
        LocalDate endTime = reqVo.getEndTime();
        LocalDate now = LocalDate.now();
        if (startTime == null) {
            startTime = now.with(ChronoField.DAY_OF_WEEK, 1);
        }
        if (endTime == null) {
//            endTime = now.with(ChronoField.DAY_OF_WEEK, 7);
            endTime = startTime.plusDays(7);
        } else {
            endTime = endTime.plusDays(1);
        }
        String materName = StringUtils.hasText(reqVo.getMaterName()) ? reqVo.getMaterName() : "";
        String[] split = materName.split(",");
        List<String> list = Arrays.asList(split);
        // 整加交付区
        QueryWrapper<ApsWorkDetail> zzQw = new QueryWrapper<>();
        zzQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", startTime);
        zzQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            zzQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            zzQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            zzQw.eq("o.order_status", 9);
        }
        zzQw.ne("o.order_status", 10);
        zzQw.in("o.order_type", 1,3,4);
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            zzQw.and(wp -> {
                int i = 0;
                wp.like("awd.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("awd.mater_name", list.get(i));
                }
            });
        }
        zzQw.and(wp->wp.like("awd.mater_no", "P5").or().like("awd.mater_no", "P4").in("awd.phase_code", "ZT","ZZ","PT").or().like("awd.mater_no", "P3").in("awd.phase_code", "ZT","ZZ").or().like("awd.mater_name", "侧护板").eq("awd.phase_code", "PT").or().like("awd.mater_name", "挡板组件").eq("awd.phase_code", "PT"));
//        zzQw.like("awd.mater_no", "P5").or().like("awd.mater_no", "P4").in("awd.phase_code", "ZT","ZZ","PT").or().like("awd.mater_no", "P3").in("awd.phase_code", "ZT","ZZ");
        List<WeekWorkDetail> zzDayDetailList = apsWorkDetailMapper.getDayDetailOne(zzQw);
        List<WeekWorkInfoVo> zzWeekWork = backListWeekWork(zzDayDetailList, startTime, endTime, 1);
        resVo.setZzList(zzWeekWork);

//        List<WeekWorkInfoVo> zzWeekWork = apsWorkDetailMapper.getWeekWorkOne(zzQw);
//        if (CollUtil.isNotEmpty(zzWeekWork)) {
//            zzWeekWork = getDetail(zzWeekWork, startTime, endTime);
//        } else {
//            zzWeekWork = new ArrayList<>();
//        }
//        resVo.setZzList(zzWeekWork);
        // 焊接区
        QueryWrapper<ApsWorkDetail> hjQw = new QueryWrapper<>();
        hjQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", startTime);
        hjQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            hjQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            hjQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            hjQw.eq("o.order_status", 9);
        }
        hjQw.ne("o.order_status", 10);
        hjQw.in("o.order_type", 1,3,4);
        hjQw.in("awd.phase_code", "PZ", "WH");
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            hjQw.and(wp -> {
                int i = 0;
                wp.like("awd.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("awd.mater_name", list.get(i));
                }
            });
        }
        hjQw.and(wp->wp.like("awd.mater_no", "P1").or().like("awd.mater_no", "P2").or().like("awd.mater_no", "P3").or().like("awd.mater_no", "P4").or().like("awd.mater_name", "侧护板").or().like("awd.mater_name", "挡板组件"));
        List<WeekWorkDetail> hjDayDetailList = apsWorkDetailMapper.getDayDetailOne(hjQw);
        List<WeekWorkInfoVo> hjWeekWork = backListWeekWork(hjDayDetailList, startTime, endTime, 2);
        resVo.setHjList(hjWeekWork);

//        List<WeekWorkInfoVo> hjWeekWork = apsWorkDetailMapper.getWeekWorkOne(hjQw);
//        if (CollUtil.isNotEmpty(hjWeekWork)) {
//            hjWeekWork = getDetail(hjWeekWork, startTime, endTime);
//        } else {
//            hjWeekWork = new ArrayList<>();
//        }
//        resVo.setHjList(hjWeekWork);
        // 配套区
        QueryWrapper<ApsWorkDetail> ptQw = new QueryWrapper<>();
        ptQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -8 HOUR))", startTime);
        ptQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -8 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            ptQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            ptQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            ptQw.eq("o.order_status", 9);
        }
        ptQw.ne("o.order_status", 10);
        ptQw.in("o.order_type", 1,3,4);
        ptQw.eq("awd.phase_code", "PZ");
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            ptQw.and(wp -> {
                int i = 0;
                wp.like("awd.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("awd.mater_name", list.get(i));
                }
            });
        }
        ptQw.and(wp->wp.like("awd.mater_no", "P1").or().like("awd.mater_no", "P2").or().like("awd.mater_no", "P3").or().like("awd.mater_name", "侧护板").or().like("awd.mater_name", "挡板组件"));
        List<WeekWorkDetail> ptDayDetailList = apsWorkDetailMapper.getDayDetailTwo(ptQw);
        List<WeekWorkInfoVo> ptWeekWork = backListWeekWork(ptDayDetailList, startTime, endTime, 3);
        resVo.setPtList(ptWeekWork);

//        List<WeekWorkInfoVo> ptWeekWork = apsWorkDetailMapper.getWeekWorkOne(ptQw);
//        if (CollUtil.isNotEmpty(ptWeekWork)) {
//            ptWeekWork = getPtDetail(ptWeekWork, startTime, endTime);
//        } else {
//            ptWeekWork = new ArrayList<>();
//        }
//        resVo.setPtList(ptWeekWork);
        // 下料区
        QueryWrapper<ApsWorkDetail> xlQw = new QueryWrapper<>();
        xlQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -24 HOUR))", startTime);
        xlQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -24 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            xlQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            xlQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            xlQw.eq("o.order_status", 9);
        }
        xlQw.ne("o.order_status", 10);
        xlQw.in("o.order_type", 1,3,4);
        xlQw.eq("awd.phase_code", "PZ");
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            xlQw.and(wp -> {
                int i = 0;
                wp.like("awd.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("awd.mater_name", list.get(i));
                }
            });
        }
        xlQw.and(wp->wp.like("awd.mater_no", "P1").or().like("awd.mater_name", "侧护板").or().like("awd.mater_name", "挡板组件"));
        List<WeekWorkDetail> xlDayDetailList = apsWorkDetailMapper.getDayDetailThree(xlQw);
        List<WeekWorkInfoVo> xlWeekWork = backListWeekWork(xlDayDetailList, startTime, endTime, 3);
        resVo.setXlList(xlWeekWork);

//        List<WeekWorkInfoVo> xlWeekWork = apsWorkDetailMapper.getWeekWorkOne(xlQw);
//        if (CollUtil.isNotEmpty(xlWeekWork)) {
//            xlWeekWork = getXlDetail(xlWeekWork, startTime, endTime);
//        } else {
//            xlWeekWork = new ArrayList<>();
//        }
//        resVo.setXlList(xlWeekWork);
        return resVo;
    }

    public BaseListResVo<WeekWorkInfoVo> getZjList(WeekWorkReqVo reqVo) {
        LocalDate startTime = reqVo.getStartTime();
        LocalDate endTime = reqVo.getEndTime();
        LocalDate now = LocalDate.now();
        if (startTime == null) {
            startTime = now.with(ChronoField.DAY_OF_WEEK, 1);
        }
        if (endTime == null) {
//            endTime = now.with(ChronoField.DAY_OF_WEEK, 7);
            endTime = startTime.plusDays(7);
        } else {
            endTime = endTime.plusDays(1);
        }
        String materName = StringUtils.hasText(reqVo.getMaterName()) ? reqVo.getMaterName() : "";
        materName = materName.replace("侧护板", "侧板");
        String[] split = materName.split(",");
        List<String> list = Arrays.asList(split);
        // 整加交付区
        QueryWrapper<ApsWorkDetail> zzQw = new QueryWrapper<>();
        zzQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", startTime);
        zzQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            zzQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            zzQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            zzQw.eq("o.order_status", 9);
        }
        zzQw.ne("o.order_status", 10);
        zzQw.in("o.order_type", 1,3,4);
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            zzQw.and(wp -> {
                int i = 0;
                wp.like("o.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("o.mater_name", list.get(i));
                }
            });
        }
        /*zzQw.and(wp->wp.like("awd.mater_no", "P5")
                .or()
                .like("awd.mater_no", "P4").in("awd.phase_code", "ZT","ZZ","PT")
                .or()
                .like("awd.mater_no", "P3").in("awd.phase_code", "ZT","ZZ")
                .or()
                .like("awd.mater_name", "侧护板").eq("awd.phase_code", "PT")
                .or()
                .like("awd.mater_name", "挡板组件").eq("awd.phase_code", "PT"));*/
        zzQw.and(wp->wp.like("awd.mater_no", "P5")
                .or()
                .like("awd.mater_no", "P4").in("awd.phase_code", "ZT","ZZ","PT")
                .or()
                .like("awd.mater_no", "P3").in("awd.phase_code", "ZT","ZZ"));
//        zzQw.like("awd.mater_no", "P5").or().like("awd.mater_no", "P4").in("awd.phase_code", "ZT","ZZ","PT").or().like("awd.mater_no", "P3").in("awd.phase_code", "ZT","ZZ");
        List<WeekWorkDetail> zzDayDetailList = apsWorkDetailMapper.getDayDetailOne(zzQw);
        List<WeekWorkInfoVo> zzWeekWork = backListWeekWork(zzDayDetailList, startTime, endTime, 1);

        QueryWrapper<ApsWorkDetail> zzCLQw = new QueryWrapper<>();
        zzCLQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", startTime);
        zzCLQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            zzCLQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            zzCLQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            zzCLQw.eq("o.order_status", 9);
        }
        zzCLQw.ne("o.order_status", 10);
        zzCLQw.in("o.order_type", 1,3,4);
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            zzCLQw.and(wp -> {
                int i = 0;
                wp.like("o.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("o.mater_name", list.get(i));
                }
            });
        }
        zzCLQw.and(wp->wp.like("o.mater_name", "侧板").eq("awd.phase_code", "PT")
                .or()
                .like("o.mater_name", "连杆").eq("awd.phase_code", "PT"));
        List<WeekWorkDetail> zzDayDetailCLList = apsWorkDetailMapper.getDayDetailOne(zzCLQw);
        List<WeekWorkInfoVo> zzWeekWorkCL = backListWeekWorkCL(zzDayDetailCLList, startTime, endTime, 1);

        List<WeekWorkInfoVo> resList = new ArrayList<>();
        resList.addAll(zzWeekWork);
        resList.addAll(zzWeekWorkCL);
        resList.sort(Comparator.comparing(WeekWorkInfoVo::getConstructNo).thenComparing(WeekWorkInfoVo::getMaterCode));

//        List<WeekWorkInfoVo> collect = zzWeekWork.stream().sorted(Comparator.comparing(WeekWorkInfoVo::getConstructNo).thenComparing(WeekWorkInfoVo::getMaterCode)).collect(Collectors.toList());

        return BaseListResVo.of(resList, WeekWorkInfoVo.class);
    }

    public BaseListResVo<WeekWorkInfoVo> getHjList(WeekWorkReqVo reqVo) {
        LocalDate startTime = reqVo.getStartTime();
        LocalDate endTime = reqVo.getEndTime();
        LocalDate now = LocalDate.now();
        if (startTime == null) {
            startTime = now.with(ChronoField.DAY_OF_WEEK, 1);
        }
        if (endTime == null) {
//            endTime = now.with(ChronoField.DAY_OF_WEEK, 7);
            endTime = startTime.plusDays(7);
        } else {
            endTime = endTime.plusDays(1);
        }
        String materName = StringUtils.hasText(reqVo.getMaterName()) ? reqVo.getMaterName() : "";
        materName = materName.replace("侧护板", "侧板");
        String[] split = materName.split(",");
        List<String> list = Arrays.asList(split);
        QueryWrapper<ApsWorkDetail> hjQw = new QueryWrapper<>();
        hjQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", startTime);
        hjQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            hjQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            hjQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            hjQw.eq("o.order_status", 9);
        }
        hjQw.ne("o.order_status", 10);
        hjQw.in("o.order_type", 1,3,4);
        hjQw.in("awd.phase_code", "PZ", "WH");
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            hjQw.and(wp -> {
                int i = 0;
                wp.like("o.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("o.mater_name", list.get(i));
                }
            });
        }
        /*hjQw.and(wp->wp.like("awd.mater_no", "P1")
                .or()
                .like("awd.mater_no", "P2")
                .or()
                .like("awd.mater_no", "P3")
                .or()
                .like("awd.mater_no", "P4")
                .or()
                .like("awd.mater_name", "侧护板")
                .or()
                .like("awd.mater_name", "挡板组件"));*/
        hjQw.and(wp->wp.like("awd.mater_no", "P1")
                        .or()
                        .like("awd.mater_no", "P2")
                        .or()
                        .like("awd.mater_no", "P3")
                        .or()
                        .like("awd.mater_no", "P4"));
        List<WeekWorkDetail> hjDayDetailList = apsWorkDetailMapper.getDayDetailOne(hjQw);
        List<WeekWorkInfoVo> hjWeekWork = backListWeekWork(hjDayDetailList, startTime, endTime, 2);

        QueryWrapper<ApsWorkDetail> hjCLQw = new QueryWrapper<>();
        hjCLQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", startTime);
        hjCLQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -6 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            hjCLQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            hjCLQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            hjCLQw.eq("o.order_status", 9);
        }
        hjCLQw.ne("o.order_status", 10);
        hjCLQw.in("o.order_type", 1,3,4);
        hjCLQw.in("awd.phase_code", "PZ", "WH");
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            hjCLQw.and(wp -> {
                int i = 0;
                wp.like("o.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("o.mater_name", list.get(i));
                }
            });
        }
        hjCLQw.and(wp->wp.like("o.mater_name", "侧板")
                .or()
                .like("o.mater_name", "连杆"));
        List<WeekWorkDetail> hjDayDetailCLList = apsWorkDetailMapper.getDayDetailOne(hjCLQw);
        List<WeekWorkInfoVo> hjWeekWorkCL = backListWeekWorkCL(hjDayDetailCLList, startTime, endTime, 2);

        List<WeekWorkInfoVo> resList = new ArrayList<>();
        resList.addAll(hjWeekWork);
        resList.addAll(hjWeekWorkCL);
        resList.sort(Comparator.comparing(WeekWorkInfoVo::getConstructNo).thenComparing(WeekWorkInfoVo::getMaterCode));

        return BaseListResVo.of(resList, WeekWorkInfoVo.class);
    }

    public BaseListResVo<WeekWorkInfoVo> getPtList(WeekWorkReqVo reqVo) {
        LocalDate startTime = reqVo.getStartTime();
        LocalDate endTime = reqVo.getEndTime();
        LocalDate now = LocalDate.now();
        if (startTime == null) {
            startTime = now.with(ChronoField.DAY_OF_WEEK, 1);
        }
        if (endTime == null) {
//            endTime = now.with(ChronoField.DAY_OF_WEEK, 7);
            endTime = startTime.plusDays(7);
        } else {
            endTime = endTime.plusDays(1);
        }
        String materName = StringUtils.hasText(reqVo.getMaterName()) ? reqVo.getMaterName() : "";
        materName = materName.replace("侧护板", "侧板");
        String[] split = materName.split(",");
        List<String> list = Arrays.asList(split);
        QueryWrapper<ApsWorkDetail> ptQw = new QueryWrapper<>();
        ptQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -8 HOUR))", startTime);
        ptQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -8 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            ptQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            ptQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            ptQw.eq("o.order_status", 9);
        }
        ptQw.ne("o.order_status", 10);
        ptQw.in("o.order_type", 1,3,4);
        ptQw.eq("awd.phase_code", "PZ");
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            ptQw.and(wp -> {
                int i = 0;
                wp.like("o.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("o.mater_name", list.get(i));
                }
            });
        }
        /*ptQw.and(wp->wp.like("awd.mater_no", "P1")
                .or()
                .like("awd.mater_no", "P2")
                .or()
                .like("awd.mater_no", "P3")
                .or()
                .like("awd.mater_name", "侧护板")
                .or()
                .like("awd.mater_name", "挡板组件"));*/
        ptQw.and(wp->wp.like("awd.mater_no", "P1")
                        .or()
                        .like("awd.mater_no", "P2")
                        .or()
                        .like("awd.mater_no", "P3"));
        List<WeekWorkDetail> ptDayDetailList = apsWorkDetailMapper.getDayDetailTwo(ptQw);
        List<WeekWorkInfoVo> ptWeekWork = backListWeekWork(ptDayDetailList, startTime, endTime, 3);

        QueryWrapper<ApsWorkDetail> ptCLQw = new QueryWrapper<>();
        ptCLQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -8 HOUR))", startTime);
        ptCLQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -8 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            ptCLQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            ptCLQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            ptCLQw.eq("o.order_status", 9);
        }
        ptCLQw.ne("o.order_status", 10);
        ptCLQw.in("o.order_type", 1,3,4);
        ptCLQw.eq("awd.phase_code", "PZ");
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            ptCLQw.and(wp -> {
                int i = 0;
                wp.like("o.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("o.mater_name", list.get(i));
                }
            });
        }
        ptCLQw.and(wp->wp.like("o.mater_name", "侧板")
                .or()
                .like("o.mater_name", "连杆"));
        List<WeekWorkDetail> ptDayDetailCLList = apsWorkDetailMapper.getDayDetailTwo(ptCLQw);
        List<WeekWorkInfoVo> ptWeekWorkCL = backListWeekWorkCL(ptDayDetailCLList, startTime, endTime, 3);

        List<WeekWorkInfoVo> resList = new ArrayList<>();
        resList.addAll(ptWeekWork);
        resList.addAll(ptWeekWorkCL);
        resList.sort(Comparator.comparing(WeekWorkInfoVo::getConstructNo).thenComparing(WeekWorkInfoVo::getMaterCode));

        for (WeekWorkInfoVo vo : resList) {
            vo.setPhaseCode("PT");
            vo.setPhaseName("配套");
        }
        return BaseListResVo.of(resList, WeekWorkInfoVo.class);
    }

    public BaseListResVo<WeekWorkInfoVo> getXlList(WeekWorkReqVo reqVo) {
        LocalDate startTime = reqVo.getStartTime();
        LocalDate endTime = reqVo.getEndTime();
        LocalDate now = LocalDate.now();
        if (startTime == null) {
            startTime = now.with(ChronoField.DAY_OF_WEEK, 1);
        }
        if (endTime == null) {
//            endTime = now.with(ChronoField.DAY_OF_WEEK, 7);
            endTime = startTime.plusDays(7);
        } else {
            endTime = endTime.plusDays(1);
        }
        String materName = StringUtils.hasText(reqVo.getMaterName()) ? reqVo.getMaterName() : "";
        materName = materName.replace("侧护板", "侧板");
        String[] split = materName.split(",");
        List<String> list = Arrays.asList(split);
        QueryWrapper<ApsWorkDetail> xlQw = new QueryWrapper<>();
        xlQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -24 HOUR))", startTime);
        xlQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -24 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            xlQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            xlQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            xlQw.eq("o.order_status", 9);
        }
        xlQw.ne("o.order_status", 10);
        xlQw.in("o.order_type", 1,3,4);
        xlQw.eq("awd.phase_code", "PZ");
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            xlQw.and(wp -> {
                int i = 0;
                wp.like("o.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("o.mater_name", list.get(i));
                }
            });
        }
        /*xlQw.and(wp->wp.like("awd.mater_no", "P1")
                .or()
                .like("awd.mater_name", "侧护板")
                .or()
                .like("awd.mater_name", "挡板组件"));*/
        xlQw.like("awd.mater_no", "P1");
        List<WeekWorkDetail> xlDayDetailList = apsWorkDetailMapper.getDayDetailThree(xlQw);
        List<WeekWorkInfoVo> xlWeekWork = backListWeekWork(xlDayDetailList, startTime, endTime, 3);

        QueryWrapper<ApsWorkDetail> xlCLQw = new QueryWrapper<>();
        xlCLQw.ge("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -24 HOUR))", startTime);
        xlCLQw.lt("DATE(DATE_ADD(awd.plan_start_date_time, INTERVAL -24 HOUR))", endTime);
        if (StringUtils.hasText(reqVo.getConstructNo())) {
            xlCLQw.like("o.construct_no", reqVo.getConstructNo());
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 1) {
            xlCLQw.ne("o.order_status", 9);
        }
        if (reqVo.getOrderStatus() != null && reqVo.getOrderStatus() == 2) {
            xlCLQw.eq("o.order_status", 9);
        }
        xlCLQw.ne("o.order_status", 10);
        xlCLQw.in("o.order_type", 1,3,4);
        xlCLQw.eq("awd.phase_code", "PZ");
        if (CollUtil.isNotEmpty(list) && StringUtils.hasText(list.get(0))) {
            xlCLQw.and(wp -> {
                int i = 0;
                wp.like("o.mater_name", list.get(i));
                i++;
                for (; i < list.size(); i++) {
                    wp.or().like("o.mater_name", list.get(i));
                }
            });
        }
        xlCLQw.and(wp->wp.like("o.mater_name", "侧板")
                .or()
                .like("o.mater_name", "连杆"));
        List<WeekWorkDetail> xlDayDetailCLList = apsWorkDetailMapper.getDayDetailThree(xlCLQw);
        List<WeekWorkInfoVo> xlWeekWorkCL = backListWeekWorkCL(xlDayDetailCLList, startTime, endTime, 3);

        List<WeekWorkInfoVo> resList = new ArrayList<>();
        resList.addAll(xlWeekWork);
        resList.addAll(xlWeekWorkCL);
        resList.sort(Comparator.comparing(WeekWorkInfoVo::getConstructNo).thenComparing(WeekWorkInfoVo::getMaterCode));

        for (WeekWorkInfoVo vo : resList) {
            vo.setPhaseCode("XL");
            vo.setPhaseName("下料");
        }
        return BaseListResVo.of(resList, WeekWorkInfoVo.class);
    }

    /*public List<WeekWorkInfoVo> getDetail(List<WeekWorkInfoVo> reqVo, LocalDate startTime, LocalDate endTime) {
//        long until = startTime.until(endTime, ChronoUnit.DAYS);

        QueryWrapper<ApsWorkDetail> dQw = new QueryWrapper<>();
        for (WeekWorkInfoVo oneVo : reqVo) {
            BigDecimal weekPlan = BigDecimal.ZERO;
            List<WeekWorkDetail> dayDetailList = new ArrayList<>();
//            final int ifinal = i;
            dQw.or(wr ->{
                wr.eq("o.construct_no", oneVo.getConstructNo());
                wr.eq("awd.mater_no", oneVo.getMaterCode());
                wr.eq("awd.phase_code", oneVo.getPhaseCode());
                wr.ge("DATE(awd.plan_start_date_time)", startTime);
                wr.lt("DATE(awd.plan_start_date_time)", endTime);
            });
//            for (int i = 1; i <= until; i++) {
//                if (detail == null) {
//                    detail = new WeekWorkDetail();
//                    detail.setCount(BigDecimal.ZERO);
//                }
//                String timeString = startTime.plusDays(j).toString();
//                String[] split = timeString.split("-");
//                detail.setDateTime(split[1] + "月" + split[2] + "日");
//                detail.setDate(startTime.plusDays(j));
//                weekPlan = weekPlan.add(detail.getCount());
//                detail.setPid(oneVo.getId());
//                dayDetailList.add(detail);
//            }
//            oneVo.setWeekPlan(weekPlan);
//            oneVo.setDayDetail(dayDetailList);
//            oneVo.setPerWeight(oneVo.getPerWeight().divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP));
//            oneVo.setPerWorkTime(oneVo.getPerWorkTime().divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP));
//            oneVo.setPlanWeight(oneVo.getPerWeight().multiply(weekPlan).setScale(2, RoundingMode.HALF_UP));
//            oneVo.setTotalWorkTime(oneVo.getPerWorkTime().multiply(weekPlan).setScale(2, RoundingMode.HALF_UP));
//            oneVo.setLeftCount(oneVo.getTotalCount().subtract(oneVo.getFinishCount()).setScale(0, RoundingMode.HALF_UP));
        }
        List<WeekWorkDetail> dayDetailList = apsWorkDetailMapper.getDayDetail(dQw);

        return reqVo;
    }*/

    /*public List<WeekWorkInfoVo> getPtDetail(List<WeekWorkInfoVo> reqVo, LocalDate startTime, LocalDate endTime) {
        long until = startTime.until(endTime, ChronoUnit.DAYS);
        for (WeekWorkInfoVo oneVo : reqVo) {
            BigDecimal weekPlan = BigDecimal.ZERO;
            List<WeekWorkDetail> dayDetailList = new ArrayList<>();
            for (int i = 1; i <= until; i++) {
                QueryWrapper<ApsWorkDetail> dQw = new QueryWrapper<>();
                dQw.eq("o.construct_no", oneVo.getConstructNo());
                dQw.eq("awd.mater_no", oneVo.getMaterCode());
                dQw.eq("awd.phase_code", oneVo.getPhaseCode());
                int j = i - 1;
                dQw.ge("awd.plan_start_date_time", startTime.plusDays(j) + " 08:00:00");
                dQw.lt("awd.plan_start_date_time", startTime.plusDays(i) + " 08:00:00");
                WeekWorkDetail detail = apsWorkDetailMapper.getDayDetail(dQw);
                if (detail == null) {
                    detail = new WeekWorkDetail();
                    detail.setCount(BigDecimal.ZERO);
                }
                String timeString = startTime.plusDays(j).toString();
                String[] split = timeString.split("-");
                detail.setDateTime(split[1] + "月" + split[2] + "日");
                detail.setDate(startTime.plusDays(j));
                weekPlan = weekPlan.add(detail.getCount());
                detail.setPid(oneVo.getId());
                dayDetailList.add(detail);
            }
            oneVo.setWeekPlan(weekPlan);
            oneVo.setDayDetail(dayDetailList);
            oneVo.setPerWeight(oneVo.getPerWeight().divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP));
            oneVo.setPerWorkTime(oneVo.getPerWorkTime().divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP));
            oneVo.setPlanWeight(oneVo.getPerWeight().multiply(weekPlan).setScale(2, RoundingMode.HALF_UP));
            oneVo.setTotalWorkTime(oneVo.getPerWorkTime().multiply(weekPlan).setScale(2, RoundingMode.HALF_UP));
            oneVo.setLeftCount(oneVo.getTotalCount().subtract(oneVo.getFinishCount()).setScale(0, RoundingMode.HALF_UP));
        }
        return reqVo;
    }

    public List<WeekWorkInfoVo> getXlDetail(List<WeekWorkInfoVo> reqVo, LocalDate startTime, LocalDate endTime) {
        long until = startTime.until(endTime, ChronoUnit.DAYS);
        for (WeekWorkInfoVo oneVo : reqVo) {
            BigDecimal weekPlan = BigDecimal.ZERO;
            List<WeekWorkDetail> dayDetailList = new ArrayList<>();
            for (int i = 1; i <= until; i++) {
                QueryWrapper<ApsWorkDetail> dQw = new QueryWrapper<>();
                dQw.eq("o.construct_no", oneVo.getConstructNo());
                dQw.eq("awd.mater_no", oneVo.getMaterCode());
                dQw.eq("awd.phase_code", oneVo.getPhaseCode());
                int j = i - 1;
                dQw.ge("awd.plan_start_date_time", startTime.plusDays(j).plusDays(1));
                dQw.lt("awd.plan_start_date_time", startTime.plusDays(i).plusDays(1));
                WeekWorkDetail detail = apsWorkDetailMapper.getDayDetail(dQw);
                if (detail == null) {
                    detail = new WeekWorkDetail();
                    detail.setCount(BigDecimal.ZERO);
                }
                String timeString = startTime.plusDays(j).toString();
                String[] split = timeString.split("-");
                detail.setDateTime(split[1] + "月" + split[2] + "日");
                detail.setDate(startTime.plusDays(j));
                weekPlan = weekPlan.add(detail.getCount());
                detail.setPid(oneVo.getId());
                dayDetailList.add(detail);
            }
            oneVo.setWeekPlan(weekPlan);
            oneVo.setDayDetail(dayDetailList);
            oneVo.setPerWeight(oneVo.getPerWeight().divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP));
            oneVo.setPerWorkTime(oneVo.getPerWorkTime().divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP));
            oneVo.setPlanWeight(oneVo.getPerWeight().multiply(weekPlan).setScale(2, RoundingMode.HALF_UP));
            oneVo.setTotalWorkTime(oneVo.getPerWorkTime().multiply(weekPlan).setScale(2, RoundingMode.HALF_UP));
            oneVo.setLeftCount(oneVo.getTotalCount().subtract(oneVo.getFinishCount()).setScale(0, RoundingMode.HALF_UP));
        }
        return reqVo;
    }*/

    public List<WeekWorkInfoVo> backListWeekWork(List<WeekWorkDetail> dayDetailList, LocalDate startDate, LocalDate endDate, Integer type) {
        if (CollUtil.isEmpty(dayDetailList)) {
            return new ArrayList<>();
        }
        long until = startDate.until(endDate, ChronoUnit.DAYS);
        // 根据pid分组成map
        Map<Integer, List<WeekWorkDetail>> map = dayDetailList.stream().collect(Collectors.groupingBy(WeekWorkDetail::getPid));
        List<Integer> phaseIds = dayDetailList.stream().map(WeekWorkDetail::getPid).collect(Collectors.toList());
        QueryWrapper<ApsWorkDetail> qw = new QueryWrapper<>();
        qw.in("awd.phase_id", phaseIds);
//        List<WeekWorkInfoVo> backList = apsWorkDetailMapper.getBackList(qw);
        List<WeekWorkInfoVo> backList = new ArrayList<>();
        if (type == 1) {
            backList = apsWorkDetailMapper.getBackListOne(qw);
        } else if (type == 2) {
            backList = apsWorkDetailMapper.getBackListTwo(qw);
        } else if (type == 3) {
            backList = apsWorkDetailMapper.getBackListThree(qw);
        }
        for (WeekWorkInfoVo vo : backList) {
            List<WeekWorkDetail> details = map.get(vo.getId());
            Map<LocalDate, WeekWorkDetail> collect = details.stream().collect(Collectors.toMap(WeekWorkDetail::getDate, Function.identity()));
            BigDecimal weekPlan = BigDecimal.ZERO;
            for (int i = 0; i < until; i++) {
                LocalDate day = startDate.plusDays(i);
                WeekWorkDetail detail = collect.get(day);
                if (ObjectUtils.isEmpty(detail)) {
                    detail = new WeekWorkDetail();
                    detail.setDate(day);
                    detail.setCount(BigDecimal.ZERO);
                    detail.setPid(vo.getId());
                    details.add(i, detail);
                }
                String timeString = day.toString();
                String[] split = timeString.split("-");
                detail.setDateTime(split[1] + "月" + split[2] + "日");
                weekPlan = weekPlan.add(detail.getCount());
            }
            vo.setDayDetail(details);
            vo.setWeekPlan(weekPlan);
            vo.setPerWeight(vo.getPerWeight().divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP));
            vo.setPerWorkTime(vo.getPerWorkTime().divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP));
            vo.setPlanWeight(vo.getPerWeight().multiply(weekPlan).setScale(2, RoundingMode.HALF_UP));
            vo.setTotalWorkTime(vo.getPerWorkTime().multiply(weekPlan).setScale(2, RoundingMode.HALF_UP));
            vo.setLeftCount(vo.getTotalCount().subtract(vo.getFinishCount()).setScale(0, RoundingMode.HALF_UP));
        }
        return backList;
    }

    public List<WeekWorkInfoVo> backListWeekWorkCL(List<WeekWorkDetail> dayDetailList, LocalDate startDate, LocalDate endDate, Integer type) {
        if (CollUtil.isEmpty(dayDetailList)) {
            return new ArrayList<>();
        }
        long until = startDate.until(endDate, ChronoUnit.DAYS);
        // 根据pid分组成map
        Map<Integer, List<WeekWorkDetail>> map = dayDetailList.stream().collect(Collectors.groupingBy(WeekWorkDetail::getPid));
        List<Integer> phaseIds = dayDetailList.stream().map(WeekWorkDetail::getPid).collect(Collectors.toList());
        QueryWrapper<ApsWorkDetail> qw = new QueryWrapper<>();
        qw.in("awd.phase_id", phaseIds);
//        List<WeekWorkInfoVo> backList = apsWorkDetailMapper.getBackListCL(qw);
        List<WeekWorkInfoVo> backList = new ArrayList<>();
        if (type == 1) {
            backList = apsWorkDetailMapper.getBackListOne(qw);
        } else if (type == 2) {
            backList = apsWorkDetailMapper.getBackListTwo(qw);
        } else if (type == 3) {
            backList = apsWorkDetailMapper.getBackListThree(qw);
        }
        for (WeekWorkInfoVo vo : backList) {
            List<WeekWorkDetail> details = map.get(vo.getId());
            Map<LocalDate, WeekWorkDetail> collect = details.stream().collect(Collectors.toMap(WeekWorkDetail::getDate, Function.identity()));
            BigDecimal weekPlan = BigDecimal.ZERO;
            for (int i = 0; i < until; i++) {
                LocalDate day = startDate.plusDays(i);
                WeekWorkDetail detail = collect.get(day);
                if (ObjectUtils.isEmpty(detail)) {
                    detail = new WeekWorkDetail();
                    detail.setDate(day);
                    detail.setCount(BigDecimal.ZERO);
                    detail.setPid(vo.getId());
                    details.add(i, detail);
                }
                String timeString = day.toString();
                String[] split = timeString.split("-");
                detail.setDateTime(split[1] + "月" + split[2] + "日");
                weekPlan = weekPlan.add(detail.getCount());
            }
            vo.setDayDetail(details);
            vo.setWeekPlan(weekPlan);
            vo.setPerWeight(vo.getPerWeight().divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP));
            vo.setPerWorkTime(vo.getPerWorkTime().divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP));
            vo.setPlanWeight(vo.getPerWeight().multiply(weekPlan).setScale(2, RoundingMode.HALF_UP));
            vo.setTotalWorkTime(vo.getPerWorkTime().multiply(weekPlan).setScale(2, RoundingMode.HALF_UP));
            vo.setLeftCount(vo.getTotalCount().subtract(vo.getFinishCount()).setScale(0, RoundingMode.HALF_UP));
        }
        return backList;
    }

    public BaseListResVo<PlanDetailResVo> planDetail(PlanDetailReqVo reqVo) {
        QueryWrapper<ApsWorkDetail> qw = new QueryWrapper<>();
        qw.eq("o.construct_no", reqVo.getConstructNo());
        qw.eq("awo.mater_code", reqVo.getMaterCode());
        qw.ge("awd.plan_start_date_time", reqVo.getDateTime().minusDays(8));
        qw.lt("awd.plan_start_date_time", reqVo.getDateTime().plusDays(3));
        List<PlanDetailResVo> planDetail = apsWorkDetailMapper.getPlanDetail(qw);
        for (PlanDetailResVo vo : planDetail) {
            vo.setPackageCode(PkgUtils.gen(Integer.parseInt(vo.getPackageCode())));
        }
        if (CollUtil.isNotEmpty(planDetail)) {
            planDetail = getPlanDayDetail(planDetail, reqVo.getDateTime().minusDays(8), reqVo.getDateTime().plusDays(3));
        } else {
            planDetail = new ArrayList<>();
        }
        return BaseListResVo.of(planDetail, PlanDetailResVo.class);
    }

    public BaseListResVo<PlanDetailResVo> planDetailTriangle(PlanDetailTriangleReqVo reqVo) {
        QueryWrapper<ApsWorkDetail> qw = new QueryWrapper<>();
        qw.eq("awo.pid", reqVo.getId());
        qw.ge("awd.plan_start_date_time", reqVo.getDateTime().minusDays(8));
        qw.lt("awd.plan_start_date_time", reqVo.getDateTime().plusDays(3));
        List<PlanDetailResVo> planDetail = apsWorkDetailMapper.getPlanDetail(qw);
        for (PlanDetailResVo vo : planDetail) {
            vo.setPackageCode(PkgUtils.gen(Integer.parseInt(vo.getPackageCode())));
        }
        if (CollUtil.isNotEmpty(planDetail)) {
            planDetail = getPlanDayDetail(planDetail, reqVo.getDateTime().minusDays(8), reqVo.getDateTime().plusDays(3));
        } else {
            planDetail = new ArrayList<>();
        }
        return BaseListResVo.of(planDetail, PlanDetailResVo.class);
    }

    public List<PlanDetailResVo> getPlanDayDetail(List<PlanDetailResVo> reqVo, LocalDate startTime, LocalDate endTime) {
        long until = startTime.until(endTime, ChronoUnit.DAYS);
        for (PlanDetailResVo oneVo : reqVo) {
            List<PlanDetailDay> dayDetailList = new ArrayList<>();
            for (int i = 1; i <= until; i++) {
                QueryWrapper<ApsWorkOrder> dQw = new QueryWrapper<>();
                dQw.eq("awo.id", oneVo.getId());
                int j = i - 1;
                dQw.ge("awo.plan_start_date_time", startTime.plusDays(j));
                dQw.lt("awo.plan_start_date_time", startTime.plusDays(i));
                PlanDetailDay detail = apsWorkOrderMapper.getDayPlanDetail(dQw);
                if (detail == null) {
                    detail = new PlanDetailDay();
                    detail.setCount(BigDecimal.ZERO);
                }
                detail.setDateTime(startTime.plusDays(j));
                detail.setPid(oneVo.getId());
                dayDetailList.add(detail);
            }
            oneVo.setDetailPlanList(dayDetailList);
        }
        return reqVo;
    }

    public BaseListResVo<PhaseDetailResVo> phaseDetail(PhaseDetailReqVo reqVo) {
        QueryWrapper<ApsWorkDetail> qw = new QueryWrapper<>();
        qw.eq("awd.aps_work_order_id", reqVo.getId());
        List<PhaseDetailResVo> phaseDetail = apsWorkDetailMapper.getPhaseDetail(qw);
        if (CollUtil.isNotEmpty(phaseDetail)) {
            phaseDetail = getDayPhaseDetail(phaseDetail, reqVo.getDateTime().minusDays(5), reqVo.getDateTime().plusDays(6));
        } else {
            phaseDetail = new ArrayList<>();
        }
        return BaseListResVo.of(phaseDetail, PhaseDetailResVo.class);
    }

    public List<PhaseDetailResVo> getDayPhaseDetail(List<PhaseDetailResVo> reqVo, LocalDate startTime, LocalDate endTime) {
        long until = startTime.until(endTime, ChronoUnit.DAYS);
        for (PhaseDetailResVo oneVo : reqVo) {
            List<PlanDetailDay> dayDetailList = new ArrayList<>();
            for (int i = 1; i <= until; i++) {
                QueryWrapper<ApsWorkDetail> dQw = new QueryWrapper<>();
                dQw.eq("awd.aps_work_order_id", oneVo.getId());
                dQw.eq("awd.phase_code", oneVo.getPhaseCode());
                int j = i - 1;
                dQw.ge("awd.plan_start_date_time", startTime.plusDays(j));
                dQw.lt("awd.plan_start_date_time", startTime.plusDays(i));
                PlanDetailDay detail = apsWorkDetailMapper.getDayPhaseDetail(dQw);
                if (detail == null) {
                    detail = new PlanDetailDay();
                    detail.setCount(BigDecimal.ZERO);
                }
                detail.setDateTime(startTime.plusDays(j));
                detail.setPid(oneVo.getId());
                dayDetailList.add(detail);
            }
            oneVo.setDetailPhaseList(dayDetailList);
        }
        return reqVo;
    }

    public List<OrderSubOrderResVo> subList(WeekWorkSubOrderReqVo reqVo) {

        List<ReverseOrder> list = reverseOrderMapper.lambdaQuery()
                .eq(ReverseOrder::getOrderId, reqVo.getOrderId())
                .isNotNull(ReverseOrder::getParentId)
                .orderByAsc(ReverseOrder::getSplitNum)
                .list();

        return list.stream()
                .map(e -> {
                    OrderSubOrderResVo rr = new OrderSubOrderResVo();
                    rr.setPkgCode(PkgUtils.gen(e.getSplitNum()));
                    rr.setTotal(e.getOrderQty());
                    rr.setId(e.getId());
                    rr.setStartDate(e.getDeliveryDate());
                    return rr;
                })
                .collect(Collectors.toList());
    }

}
